package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.RequiredPermission;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("")
    public String index(){
        return "department_list";
    }

    @RequestMapping("/listAll")
    @ResponseBody
    public List<Department> listAll(){
        return departmentService.selectAll();
    }

    @RequestMapping("/list")
    @ResponseBody
    @RequiresPermissions("department:list")
    @RequiredPermission("部门列表")
    public PageResult list(QueryObject qo){
        PageResult pageResult =  departmentService.query(qo);
        return pageResult;
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions("department:saveOrUpdate")
    @RequiredPermission("部门新增")
    public JSONResult saveOrUpdate(Department department){

        departmentService.saveOrUpdate(department);
        return new JSONResult("保存成功");
    }

    @RequestMapping("/updateState")
    @ResponseBody
    @RequiresPermissions("department:updateState")
    @RequiredPermission("部门离职")
    public JSONResult updateState(Long id){
        departmentService.updateState(id);
        return new JSONResult("离职成功");
    }
}
